<template>
  <div>
    <!-- 睿蓝厂端打款审核 -->
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="selectHandle"
    >
    <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="add()">新 增</el-button>
        <el-button :loading="btnState" size="small" :disabled="sw" type="primary" icon="el-icon-plus" @click="posting()">过 账</el-button>
    </template>
    </FilterTable>

    <BaseDialog
      :visible.sync="issearch"
      :title="'服务站查询'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <ownerSeacher  @close="dealercloseHandler" />
    </BaseDialog>

     <Drawer
      :visible.sync="openPopup"
      :title="popupTitle"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <addBill :actionType="actionType" :row="popupRow" @close="popupCloseHandler" />
    </Drawer>

    <!-- 查看凭证 -->
    <el-image-viewer :url-list="imageList" v-show="imagesViewShow" :on-close="closeImagesViewer"></el-image-viewer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { selectBill,factoryPosting,delBill} from '@/api/partsmanage/financialManagement/capitalAccount/index';
import ownerSeacher from '@/pages/partsmanage/claim/approve/ownerSeacher';
import addBill from '@/pages/partsmanage/financialManagement/dealerAccountAdjust/customPopup/addBill';
import { notifyAction } from '@/utils';
import ElImageViewer from 'element-ui/packages/image/src/image-viewer';
import { findFileByUrl } from '@/api/partsmanage/financialManagement/capitalAccount/index';

export default {
  name: "dealerAccountAdjust",
  mixins: [dictionary],
  components: { ownerSeacher, addBill, ElImageViewer },
  data() {
    return {
      imageList: [],
      imagesViewShow: false,
      issearch: false,
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: selectBill,
      fetchParams: {xhrAbort: true},
      openPopup:false,
      popupRow:{},
      file: false,
      popupTitle:"",
      selectRows:[],
      sw:false,
      actionType:"",
      btnState:false
    };
  },
  methods: {
    refresh(){
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    del(row){
      delBill(row).then(res =>{
        this.$notify.success({title: '提示信息',message:'删除成功!'});
        this.refresh();
      }).catch(err =>{
        this.$notify.error({title: '提示信息',message:err+';删除失败!'});
      })
    },
    posting(){
      if(this.selectRows == null || this.selectRows.length <= 0 ){
        return notifyAction('当前选中记录为空!', 'warning');
      }
      this.btnState = true;
      factoryPosting(this.selectRows).then(res =>{
        this.btnState = false;
        this.$notify.success({title: '提示信息',message:'过账成功!'});
        this.refresh();
      }).catch(err =>{
        this.$notify.error({title: '提示信息',message:err+';过账失败!'});
        this.btnState = false;
      })
    },
    selectHandle(rows){//选中的行数
      this.selectRows = rows;
      let mark = false;
      rows.forEach(function (row){
          if(row.adjustStatus == 30201002){
            mark = true;
          }
      })
      this.sw = mark;
    },
    add(){//点击新增打开弹框
      this.openPopup = true;
      this.popupRow = null;
      this.popupTitle = "新建调账单";
      this.actionType = "add";
    },
    edit(row){
      this.openPopup = true;
      this.popupRow = row;
      this.popupTitle = "编辑调账单";
      this.actionType = "edit";
    },
    async look(row){//查看按钮
      if(row.uploadFile.length <= 0 || row.uploadFile[0].url == null || row.uploadFile[0].url == ''){
        return notifyAction('当前记录凭证为空!', 'warning');
      }
      this.imagesViewShow = true;
      this.imageList = [];
      findFileByUrl(row.uploadFile[0].url).then(res => {
        console.log(res)
        this.imageList.push(res.data);
      })
    },
    closeImagesViewer() {
      this.imagesViewShow = false;
    },
    popupCloseHandler(val) {
      this.openPopup = false;
      //刷新页面
      this.refresh();
    },
    seacherFun(){
      this.issearch = true
    },
    dealercloseHandler(val){
      if(val){
        this.topFilterList.find(x=>x.fieldName=='dealerCode').initialValue=val.dealerCode;
      }
      this.issearch=false;
    },
    createTopFilterList() {
      return[
        {
          type: 'INPUT',
          label: '服务站查询',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'RANGE_DATE',
          label: '记账日期',
          fieldName: 'submitDate1|submitDate2',
          placeholder: '请选择...',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'RANGE_DATE',
          label: '过账日期',
          fieldName: 'adjustDate1|adjustDate2',
          placeholder: '请选择...',
          valueFormat: 'yyyy-MM-dd'
        },
        {
          type: 'INPUT', 
          label: '记账凭证号', 
          fieldName: 'adjustNo',
        },
        {
          type: 'SELECT', 
          label: '状态', 
          fieldName: 'adjustStatus',
          placeholder: '请选择',
          itemList: this.createDictList('3020')
        },
      ]
    },
    createTableColumns () {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          sorter: true,
          align:"center",
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 150,
          align:"center",
          render: (props, h) => {
            return (
              <div>
                <el-button disabled = {props.row.adjustStatus != 30201001} size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.look(props.row)}>
                  查看凭证
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.del(props.row)}>
                  删除
                </el-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'adjustStatus',
          sorter: true,
          filter: true,
          dictItems: this.createDictList('3020')
        },
        {
          title: '服务站代码',
          dataIndex: 'dealerCode',
          sorter: true,
          align:"center"
        },
        {
          title: '服务站名称',
          dataIndex: 'dealerName',
          sorter: true,
          align:"center"
        },
        {
          title: '往来账类别',
          dataIndex: 'inOutType',
          sorter: true,
          filter: true,
          dictItems: this.createDictList('3021')
        },
        {
          title: '记账类别',
          dataIndex: 'adjustType',
          sorter: true,
          filter: true,
          dictItems: this.createDictList('3022')
        },
        {
          title: '记账凭证号',
          dataIndex: 'adjustNo',
          sorter: true,
          align:"center"
        },
        {
          title: '金额',
          dataIndex: 'amount',
          sorter: true,
          precision: 2,
          align:"center"
        },
        {
          title: '记账方式',
          dataIndex: 'adjustWay',
          sorter: true,
          filter: true,
          dictItems: this.createDictList('3023')
        },
        {
          title: '记账时间',
          dataIndex: 'submitDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat:'yyyy-MM-dd'
        },
        {
          title: '记账人',
          dataIndex: 'submitName',
          sorter: true,
          precision: 2,
          align:"center"
        },
        {
          title: '过账时间',
          dataIndex: 'adjustDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat:'yyyy-MM-dd'
        },
        {
          title: '过账人',
          dataIndex: 'adjustName',
          sorter: true,
          precision: 2,
          align:"center"
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          align:"center"
        },
      ]
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>
